from urllib3.exceptions import InsecureRequestWarning
# 禁用安全请求警告
import requests
import requests.cookies
import json
import datetime

Platform = '王牌小说'
today = datetime.date.today()  # 今天
yester = today - datetime.timedelta(days=1)
yesterday = str(yester).replace('-', '')


def wangpai():
    try:
        requests.urllib3.disable_warnings(InsecureRequestWarning)
        r = requests.session()
        date = {
            "password": "",
            "userName": ""
        }
        date = json.dumps(date)
        header = {
            "Accept": "*/*",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Content-Length": "55",
            "Content-Type": "application/json",
            "Host": "bi.ylbook.cn",
            "Origin": "https://bi.ylbook.cn",
            "Referer": "https://bi.ylbook.cn/spa/login?from=https%3A%2F%2Fbi.ylbook.cn%2F",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36",
            "X-Requested-With": "XMLHttpRequest"
        }
        rr = r.post(url="https://bi.ylbook.cn/login", data=date, headers=header, verify=False, timeout=15)
        cookie = json.dumps(rr.headers['Set-Cookie'].replace('Path=/', '').replace(';', '').replace('"', ''))
        coolies = {
            "Cookie": cookie
        }
        aa = r.get(url="https://bi.ylbook.cn/settle/queryDayRecharge?startDay=&endDay=&status=&page=1", cookies=coolies,
                   verify=False, timeout=15)
        html = json.loads(aa.text)
        if str(html["data"]["result"]["settleData"][0]['day']) == yesterday:
            Amount_share = float(html["data"]["result"]["settleData"][0]["rechargeShare"]) / 100
            date_time = yesterday
            Amount_share = round(Amount_share, 2)
            return date_time, Amount_share, Platform
        else:
            bb = r.get(url="https://bi.ylbook.cn/stat/order/list.json?siteId=14959&page=1", cookies=coolies,
                       verify=False, timeout=15)
            ht = json.loads(bb.text)
            for y in range(len(ht['data']['list'])):
                if str(ht['data']['list'][y]['formatDay']) == str(yester):
                    a, b, c = str(float(ht['data']['list'][y]['rechargeMoney']) * 0.9).partition('.')
                    if len(c) > 2:
                        c = c[:2]
                    else:
                        c = c
                    Amount_share = a + b + c
                    date_time = str(yester)
                    return date_time, Amount_share, Platform
                else:
                    if y == len(ht['data']['list']) - 1:
                        return '', 'None', Platform
                    else:
                        pass
    except Exception:
        print("获取 王牌小说 数据失败")
        return '', 'None', Platform
    except ConnectionError:
        print("获取 王牌小说 数据 超时")
        return '', 'None', Platform